import { useRequest } from 'umi';
import { getCompanyID, getPosIDs, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { getBaseUrl } from '@/services/urlconst';

const baseUrl = getBaseUrl();

/**
 * 添加规则
 * */
export const saveRuleServices = (callback: (res: any) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        isOpen: '1',
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/purchaseStyleRuleController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    saveRuleRun: request?.run,
    saveRuleLoading: request?.loading,
  };
};

/**
 * 规则详情
 * */
export const detailRuleServices = (callback: (res: any) => void) => {
  const request = useRequest(
    (payload) => {
      let reqUrl = `${baseUrl}/purchaseStyleRuleController/details/${payload?.id}`;
      return requestFunComponFiex('GET', reqUrl);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    detailRuleRun: request?.run,
    detailRuleLoading: request?.loading,
    detailRuleInfo: request?.data?.obj,
  };
};

/**
 * 编辑规则
 * */
export const editRuleServices = (callback: (res: any) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/purchaseStyleRuleController/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    editRuleRun: request?.run,
    editRuleLoading: request?.loading,
  };
};

/**
 * 是否启用
 * */
export const enableServices = (callback: (res: any) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/purchaseStyleRuleController/enable`;

      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    enableRun: request?.run,
    enableloading: request?.loading,
  };
};
